<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="起止日期" prop="executeTeamStartTime">
          <el-date-picker v-model="queryDateArr" type="daterange" range-separator="至" value-format="yyyy-MM-dd"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="机组" prop="type">
          <el-select v-model="queryParams.type" placeholder="请选择">
              <el-option v-for="item in sysUnitList" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
          </el-select>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:count:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" max-height="430" :data="countList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="起止日期" align="center" prop="oeeCountId" /> -->
      <!-- <el-table-column label="机组信息" align="center" prop="totalRunningTime" /> -->
      <el-table-column label="运转总时长" align="center" prop="totalRunningTime" />
      <el-table-column label="实际作业时长" align="center" prop="equipmentActuationRate" />
      <el-table-column label="设备开动率" align="center" prop="facilityPower" />
      <el-table-column label="运输总量" align="center" prop="transportationCount" />
      <el-table-column label="生产总量" align="center" prop="productionCount" />
      <el-table-column label="操作效率" align="center" prop="operationalEfficiency" />
      <el-table-column label="废砼方量" align="center" prop="amountOfWasteConcreteSquare" />
      <el-table-column label="质量效率" align="center" prop="massEfficiency" />
      <el-table-column label="综合效率" align="center" prop="jointEfficiency" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:count:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:count:remove']"
          >删除</el-button>
        </template> -->
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改oee统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="运转总时长：24小时*运转天数" prop="totalRunningTime">
          <el-input v-model="form.totalRunningTime" placeholder="请输入运转总时长：24小时*运转天数" />
        </el-form-item>
        <el-form-item label="实际作业时长：" prop="equipmentActuationRate">
          <el-input v-model="form.equipmentActuationRate" placeholder="请输入实际作业时长：" />
        </el-form-item>
        <el-form-item label="设备开动率：实际作业时长/运转总时长" prop="facilityPower">
          <el-input v-model="form.facilityPower" placeholder="请输入设备开动率：实际作业时长/运转总时长" />
        </el-form-item>
        <el-form-item label="运输总量：砼单中的运输总量求和" prop="transportationCount">
          <el-input v-model="form.transportationCount" placeholder="请输入运输总量：砼单中的运输总量求和" />
        </el-form-item>
        <el-form-item label="生产总量：双控机生产总量求和" prop="productionCount">
          <el-input v-model="form.productionCount" placeholder="请输入生产总量：双控机生产总量求和" />
        </el-form-item>
        <el-form-item label="操作效率：机器核定生产量" prop="operationalEfficiency">
          <el-input v-model="form.operationalEfficiency" placeholder="请输入操作效率：机器核定生产量" />
        </el-form-item>
        <el-form-item label="废砼方量：砼单中废砼方量求和" prop="amountOfWasteConcreteSquare">
          <el-input v-model="form.amountOfWasteConcreteSquare" placeholder="请输入废砼方量：砼单中废砼方量求和" />
        </el-form-item>
        <el-form-item label="质量效率：" prop="massEfficiency">
          <el-input v-model="form.massEfficiency" placeholder="请输入质量效率：" />
        </el-form-item>
        <el-form-item label="综合效率：设备开动率*操作效率*质量效率" prop="jointEfficiency">
          <el-input v-model="form.jointEfficiency" placeholder="请输入综合效率：设备开动率*操作效率*质量效率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCount,
  getCount,
  delCount,
  addCount,
  updateCount,
} from "@/api/service/production/oeeStatistics";
import { getDicts } from "@/api/system/dict/data"
export default {
  name: "Count",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // oee统计表格数据
      countList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        selectStartTime: null,
        selectEndTime: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      sysUnitList: []
    };
  },
  async created() {
    this.getList();
    const res = await getDicts("sys_unit")
    this.sysUnitList = res.data
  },
  computed: {
    queryDateArr: {
      get() {
        if (this.queryParams?.selectStartTime) {
          return [
            this.queryParams?.selectStartTime,
            this.queryParams?.selectEndTime,
          ];
        }
        return [];
      },
      set(value) {
        this.queryParams.selectStartTime = value[0];
        this.queryParams.selectEndTime = value[1];
      },
    },
  },
  methods: {
    /** 查询oee统计列表 */
    getList() {
      this.loading = true;
      listCount(this.queryParams).then((response) => {
        this.countList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        oeeCountId: null,
        totalRunningTime: null,
        equipmentActuationRate: null,
        facilityPower: null,
        transportationCount: null,
        productionCount: null,
        operationalEfficiency: null,
        amountOfWasteConcreteSquare: null,
        massEfficiency: null,
        jointEfficiency: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.selectStartTime = null;
      this.queryParams.selectEndTime = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.oeeCountId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加oee统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const oeeCountId = row.oeeCountId || this.ids;
      getCount(oeeCountId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改oee统计";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.oeeCountId != null) {
            updateCount(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCount(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const oeeCountIds = row.oeeCountId || this.ids;
      this.$modal
        .confirm('是否确认删除oee统计编号为"' + oeeCountIds + '"的数据项？')
        .then(function () {
          return delCount(oeeCountIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/oee-count/export",
        {
          ...this.queryParams,
        },
        `count_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
